package com.naxiangfish.cn.splash;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.naxiangfish.cn.backface.BackCall;

import java.util.List;

/**
 * 作者: 赵航
 * 创建时间: 2018/8/14 10:00
 * 描述:
 */
public class SplashPagerAdapter extends PagerAdapter {

    private List<String> mUri;
    private Context mContext;

    private BackCall mBackCall;

    //设置自定义回调
    public void setBackCall(BackCall backCall) {
        this.mBackCall = backCall;
    }

    public SplashPagerAdapter(SplashActivity splashView, List<String> uri) {
        this.mUri = uri;
        this.mContext = splashView;
    }

    @Override
    public int getCount() {
        return mUri.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view==o;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//        super.destroyItem(container, position, object);
        container.removeView((View) object);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
//        View v = LayoutInflater.from(ctx).inflate(R.layout.pager , null);
        ImageView view = new ImageView(mContext);

        view.setScaleType(ImageView.ScaleType.FIT_XY);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-1, -1);
        view.setLayoutParams(params);
        Glide.with(mContext)
                .load(mUri.get(position))
//                .placeholder(R.mipmap.ic_launcher)//图片加载出来前，显示的图片
//                .error(R.mipmap.ic_launcher)//图片加载失败后，显示的图片
                .into(view);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SplashPagerAdapter.this.mBackCall.deal(position,view);
            }
        });

        container.addView(view);
        return view;
    }
}
